package demo;
import java.awt.*;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.List;

import javax.swing.*;

import com.iflytek.view.MeTest;
import com.iflytek.view.TtsSpeechView1;
import pojo.Customer;
import pojo.Work;
import servicel.ICustomerService;

import common.exception.transaction.BeanFactory;

import dao.ICustomerDao;
public class SocketThread extends Thread {
	private Socket socket;
	private JLabel lblMain;
	JTextField text2;
	JTextField text3;

	public SocketThread(Socket socket, JLabel lblMain,JTextField text2,JTextField text3) {
		this.socket = socket;
		// this.customers = customers;
		this.lblMain = lblMain;
		this.lblMain.setForeground(Color.red);//把颜色改为红色
		this.text2=text2;
		this.text3=text3;
	}

	public void run() {
		ICustomerDao dao1 = (ICustomerDao) BeanFactory
				.getPojo("customerDao");
		dao1.myflag_one();
	/*	dao1.myflag_one();*/
		BufferedReader br = null;
		PrintWriter pw = null;
		try {


			br = new BufferedReader(new InputStreamReader(socket
					.getInputStream()));
			pw = new PrintWriter(socket.getOutputStream(), true);
			String temp = null;
			int index = 0;
			while (!(temp = br.readLine()).equals("end")) {
				ICustomerService service = (ICustomerService) BeanFactory
						.getPojo("customerService");
				ICustomerDao dao = (ICustomerDao) BeanFactory
						.getPojo("customerDao");

				if ((index = temp.indexOf("vip")) != -1) {//有未受理的vip客户
					int num = service.unProcess(1);//vip未受理客户的type类型为1，num表示用户在数据库中的ID值
					if (num > 0) {
						pw.println(num + "");
						/****************************/
						Thread.sleep(1);
						if (dao.query_myflag() == 1) {
							List<Customer> list1 = dao.loadAllUsers();
							text2.setText("" + list1.size());
							List<Customer> list11 = dao.findUprocess(1);
							List<Customer> list2 = dao.findUprocess(2);
							text3.setText("" + (list11.size() + list2.size()));
						}
						/****************************/
						dao.myflag_one();
						String VIP = "请VIP业务" + num + "号顾客到"
								+ temp.substring(index + 3) + "号专柜办理业务!" ;
						lblMain.setText(VIP);
						/*	int one = dao.myflag_one();*/
//						flag=one;
						try {
							new MeTest(VIP);
							Thread.sleep(6000);

						} catch (InterruptedException e) {
							e.printStackTrace();
						}

						new TtsSpeechView1(VIP);//方言
						service.process(num, 1);//客户已处理，并更新数据库
					} else {
						pw.println(num + "");//打印用户编号
						lblMain.setText("");//清除标签体的内容
					}

					//处理普通用户的数据
				} else if ((index = temp.indexOf("normals")) != -1) {

					List<Customer> list = dao.findUprocess(1);
					if (list.size() >= 2) {

						int num = service.unProcess(1);
						if (num > 0) {
							pw.println("vip" + num);
							/****************************/
							Thread.sleep(1);
							if(dao.query_myflag()==1) {
								List<Customer> list1 = dao.loadAllUsers();
								text2.setText("" + list1.size());
								List<Customer> list11 = dao.findUprocess(1);
								List<Customer> list2 = dao.findUprocess(2);
								text3.setText("" + (list11.size() + list2.size()));
							}
							/****************************/
							dao.myflag_one();
							String normal="请普通业务" + num + "号顾客到"
									+ temp.substring(index + 7) + "号专柜办理业务!";
							lblMain.setText(normal);
//							int one = dao.myflag_one();/**/
//							this.flag=one;

							try {
								new MeTest(normal);
								Thread.sleep(6000);

							} catch (InterruptedException e) {
								e.printStackTrace();
							}
							new TtsSpeechView1(normal);
							service.process(num, 1);
						} else {
								pw.println("vip" + num);
								lblMain.setText("");


						}
					} else {
						int num = service.unProcess(2);
						if (num > 0) {
							pw.println("normals" + num);
							Thread.sleep(1);
							/****************************/
							if (dao.query_myflag() == 1) {
								List<Customer> list1 = dao.loadAllUsers();
								text2.setText("" + list1.size());
								List<Customer> list11 = dao.findUprocess(1);
								List<Customer> list2 = dao.findUprocess(2);
								text3.setText("" + (list11.size() + list2.size()));
							}
							/****************************/
							dao.myflag_one();
							String word = "请普通业务" + num + "号顾客到"
									+ temp.substring(index + 7) + "号专柜办理业务!" ;
							lblMain.setText(word);
//							int one = dao.myflag_one();
//							this.flag=one;

							try {
								new MeTest(word);
								Thread.sleep(6000);

							} catch (InterruptedException e) {
								e.printStackTrace();
							}

							new TtsSpeechView1(word);
							service.process(num, 2);
						} else {
							pw.println("normals" + num);
							lblMain.setText("中国银行宾阳分行");


					}
					}
				}
			}

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				br.close();
				pw.close();
				socket.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
}
